package com.todod.base.module.base.controller.admin.site;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.todod.base.framework.common.pojo.PageParam;
import com.todod.base.framework.common.pojo.PageResult;
import com.todod.base.framework.common.pojo.CommonResult;
import com.todod.base.framework.common.util.object.BeanUtils;
import static com.todod.base.framework.common.pojo.CommonResult.success;

import com.todod.base.framework.excel.core.util.ExcelUtils;

import com.todod.base.framework.apilog.core.annotation.ApiAccessLog;
import static com.todod.base.framework.apilog.core.enums.OperateTypeEnum.*;

import com.todod.base.module.base.controller.admin.site.vo.*;
import com.todod.base.module.base.dal.dataobject.site.SiteDO;
import com.todod.base.module.base.service.site.SiteService;

@Tag(name = "管理后台 - 现场")
@RestController
@RequestMapping("/base/site")
@Validated
@Slf4j
public class SiteController {

    @Resource
    private SiteService siteService;

    @PostMapping("/create")
    @Operation(summary = "创建现场")
    public CommonResult<String> createSite(@Valid @RequestBody SiteSaveReqVO createReqVO) {
        return success(siteService.createSite(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新现场")
    public CommonResult<Boolean> updateSite(@Valid @RequestBody SiteSaveReqVO updateReqVO) {
        siteService.updateSite(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除现场")
    public CommonResult<Boolean> deleteSite(@RequestParam("wellCode") String wellCode) {
        siteService.deleteSite(wellCode);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得现场")
    public CommonResult<SiteRespVO> getSite(@RequestParam("wellCode") String wellCode) {
        SiteDO site = siteService.getSite(wellCode);
        return success(BeanUtils.toBean(site, SiteRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得现场分页")
    public CommonResult<PageResult<SiteRespVO>> getSitePage(@Valid SitePageReqVO pageReqVO) {
        PageResult<SiteDO> pageResult = siteService.getSitePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SiteRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出现场 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSiteExcel(@Valid SitePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SiteDO> list = siteService.getSitePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "现场.xls", "数据", SiteRespVO.class,
                        BeanUtils.toBean(list, SiteRespVO.class));
    }

    /**
     * 获取现场列表信息
     **/
    @GetMapping("/getlist")
    @Operation(summary = "获取现场精简信息列表", description = "只包含被现场的用户，主要用于前端的下拉选项")
    public CommonResult<List<SiteRespVO>> getListSite(String params){
        return success(BeanUtils.toBean(siteService.getListSite(params),SiteRespVO.class));
    }


    @GetMapping("/enable")
    @Operation(summary = "启用现场")
    public CommonResult<Boolean> enable(@RequestParam("wellCode") String wellCode) {
        log.info("调用启用仪器设备控制层");
        siteService.enable(wellCode);
        return success(true);
    }

    @GetMapping("/disable")
    @Operation(summary = "禁用现场")
    public CommonResult<Boolean> dis(@RequestParam("wellCode") String wellCode) {
        log.info("调用禁用仪器设备控制层");
        siteService.disable(wellCode);
        return success(true);
    }


    @GetMapping("/getwellnum")
    @Operation(summary = "获取现场已经启用的井数量")
    public CommonResult<Integer> getWellNum() {
        return success(siteService.getWellNum());
    }



}